<?php
/**
  * Musashi - A project manager for music groups and bands
  * Copyright (C) 2010 Daniel Torres
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  */

/**
 *  This view shows a data entry/edit form for songs
 *  
 *  @author Daniel Torres
 *  @package Musashi
 */

// Make sure we have access to this page
defined('_JEXEC') or die ('Restricted access. Sorry');

// We'll need some positions and equipment. Query them here:
$model     =& $this->getModel();
$positions = $model->getRecordsForSection('positions','id,name','name');
$posCount  = count($positions);

// Get a list of positions related to this song
$sql = "SELECT idPosition FROM #__Musashi_Song_Position
        WHERE #__Musashi_Song_Position.idSong = ".$this->record->id;
            
$entries =& $model->_getList( $sql );
$posKeys = array();
    
// Extract all pieces into an array
$numKeys = count($entries);
for( $i = 0; $i < $numKeys; $i++ )
{
    $posKeys[] = $entries[$i]->idPosition;
}

?>
<script language="javascript" type="text/javascript">
<!--


// Packs selected fields into a linear structure
function packList( opt )
{
    var packedData = "";
    var index    = 0;
    var added    = 0;
        
    for( var i = 0; i < opt.length; i++)
    {            
        if( opt[i].selected )
        {
            if( added > 0){
                packedData += ",";
            }

            packedData += escape(opt[i].value);
            added++
        }
    }
        
    return packedData;        
}


function submitbutton(pressbutton)
{
    var form = document.adminForm;
    
    // If we're just canceling, allow it to happen
    if(pressbutton=='cancel')
    {
        submitform(pressbutton);
        return;
    }
    
    // Make sure the name field is not empty
    if(form.name.value == "")
    {
        alert("You must supply a name for this song");
        return;
    }
    
    // Pack the selected positions, if any
    form.posid.value = packList( form.positionlist );
    
    // Submit the form itself
    submitform(pressbutton);    
}
//-->
</script>
<form action="index.php" method="post" name="adminForm" id="adminForm" enctype="multipart/form-data">
    <div class="col width-50">
    <fieldset class="adminform" >
        <legend>Song Details</legend>
        <table class="admintable">
            <tr>
                <td width="100" align="right" class="key">
                    Name:
                </td>
                <td>
                    <input class="text_area" type="text" name="name" id="name" size="25" maxlength="25"
                           value="<?php echo $this->record->name; ?>" />
                </td>
            </tr>
            <tr>
                <td width="100" align="right" class="key">
                    Description:
                </td>
                <td>
                    <input class="text_area" type="text" name="description" id="description" size="45" maxlength="45"
                           value="<?php echo $this->record->description; ?>" />
                </td>
            </tr>
            <tr>
                <td width="100" align="right" class="key">
                    Approximate duration in minutes:
                </td>
                <td>
                    <input class="text_area" type="time" name="length" id="length" size="10" maxlength="10"
                           value="<?php echo $this->record->length; ?>" />
                </td>
            </tr>
            <tr>
                <td width="100" align="right" class="key">
                    Song sheet:
                </td>
                <?php
                    if( $this->record->sheet == "")
                    { ?>                        
                        <td>                    
                            <input type="file" name="sheet" id="sheet" size="50" maxlength="45" />
                        </td>                        
                        <?php
                    }
                    else
                    {
                        ?>
                        <td>
                            <?php echo "Current file: <b>".$this->record->sheet."</b>,<br/>change to: "; ?>
                            <input type="file" name="newsheet" id="newsheet" size="50" maxlength="45" />
                        </td>
                        <?php                        
                    }
                    ?>
            </tr>
    
        </table>
    </fieldset>
    </div>
    <div class="col width-50">
        <fieldset class="adminForm">
            <legend>Positions</legend>
            <table class="admintable" width="100%">
        
            <tr>
                <td width="100" align="right" class="key" >
                    <label for="positions">
                        Playing Positions
                        <p><h4>Select all required song positions from this list</h4></p>
                    </label>
                </td>
                <td>
                    <select name="positionlist" multiple size="7" style="width:25em;">
                    <?php
                        for( $i = 0; $i < $posCount; $i++ )
                        {
                            // Is this part of the original set?
                            $selected = "";
                            if( array_search($positions[$i]->id, $posKeys) !== false ){
                                $selected = "SELECTED";
                            }                                
                            ?>
                            <option value="<?php echo $positions[$i]->id; ?>" <?php echo $selected; ?>><?php echo $positions[$i]->name; ?></option>
                            <?php
                        }                    
                    ?>
                    </select>
                </td>
            </tr>
        </table>
    </fieldset>
        </div>
    <input type="hidden" name="option" value="com_musashi" />
    <input type="hidden" name="task" value="" />
    <input type="hidden" name="controller" value="catalog" />
    <input type="hidden" name="id" value="<?php echo $this->record->id; ?>" />
    <input type="hidden" name="sectn" value="<?php echo $this->viewName; ?>" />
    <input type="hidden" name="posid" value="" />
</form>



